function ResetCounter(){
document.getElementById('counter').value = "1";
document.getElementById('pnt_assoc').value = "";
}

function TryAgain() {
var tryagain;
tryagain = document.getElementById("tryagain");
tryagain.innerHTML = "<p><b>Upload your file:</b></p>";
}

function onSubmitForm() {
    var formDOMObj = document.frmSend;
    if (formDOMObj.attach1.value == "")
        alert("Please press the Browse button and pick a file.")
    else
        return true;
    return false;
}

function fctUpload()
{
var ValFileExt = [".xls"];

	function valExt() {
    	var attach = document.getElementById("attach1");
        if (attach.type == "file") {
            var sFileName = attach.value;
            if (sFileName.length > 0) {
                var blnValid = false;
                for (var j = 0; j < ValFileExt.length; j++) {
                    var sCurExtension = ValFileExt[j];
                    if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                        blnValid = true;
                        break;
                    }
                }

                if (!blnValid) {
                    return false;
                }
				else {
				return true;
				}
            }
        }
		
	return true;
	}	
	
	if (valExt() === true){
    document.frmSend.action = "dis2001d2.asp"
    document.frmSend.submit();             // Submit the page
    return true;
	}
	else{
	alert("Sorry, allowed extension is: " + ValFileExt.join(", "));	
		}
}

function fctNext()
{
    document.frmSend.action = "dis2001e.asp"
    document.frmSend.submit();             // Submit the page
    return true;
}

function passAssocs(){
var dynPrint;
dynPrint = document.getElementById("dynPrint").innerHTML;
pnt_assoc = document.getElementById("pnt_assoc");
pnt_assoc.value = dynPrint
}

var _validFileExtensions = [".xls", ".xlsx", ".txt"];

function Validate(oForm) {
    var arrInputs = oForm.getElementsByTagName("input");
    for (var i = 0; i < arrInputs.length; i++) {
        var oInput = arrInputs[i];
        if (oInput.type == "file") {
            var sFileName = oInput.value;
            if (sFileName.length > 0) {
                var blnValid = false;
                for (var j = 0; j < _validFileExtensions.length; j++) {
                    var sCurExtension = _validFileExtensions[j];
                    if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                        blnValid = true;
                        break;
                    }
                }

                if (!blnValid) {
                    alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
                    return false;
                }
            }
        }
    }

    return true;
}

function rstForm(){
document.getElementById("my1div").style.display = "block";
document.getElementById("addAssoc").style.display = "none";
document.getElementById("next").style.display = "none";
document.getElementById("assoc1").innerHTML = "";
document.getElementById("add_stat1").style.display = "none";
document.getElementById("add_corr1").style.display = "none";

var i;
var div;
var pntAssoc;
for(i=2; i<=counter; i++){
	div = document.getElementById("my" + i + "div");
	div.parentNode.removeChild(div);
	pntAssoc = document.getElementById("assoc" + i);
	pntAssoc.parentNode.removeChild(pntAssoc);
	}
	counter = 1;
	document.getElementById("counter").value = counter
}

function rstFields(){
sel = document.getElementById("my"+counter+"div").getElementsByTagName("select")
inp = document.getElementById("my"+counter+"div").getElementsByTagName("input")
var i;
for(i=0; i<(sel.length-1); i++){
	sel[i].value = '';
	}
var corr = sel.length - 1
sel[corr].value = 'None';

for(i=0; i<4; i++){
	inp[i].value = '0.00';
	}
inp[4].value = '0.0';
}

function clickclear(thisfield, defaulttext, starttext) {
if (thisfield.value == defaulttext) {
thisfield.value = starttext;
}
}
function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}

{
function fctDecimalVal()
  {
tecla = event.keyCode;
if (tecla === 46 || (tecla >= 48 && tecla <= 57))
    {
    return true;
    }
else
    {
    return false;
    }
  }
}


function pntHidden(){
	var keys = ['Disease: ','City: ','Province: ','Country: ','Continent: ','Latitude: ','Longitude: ','Ethnicity: ','Controls: ','Special Controls: ','Patients: ','Corresponding Author: ','E-mail: ','Authors: ','Study Year: ','AFND Population Name: ','PMID: ','Extra Notes: '];
	var hidden = document.getElementById("pnt_val").value;
	var values = hidden.split("$");
	var i=0;
	var datA = "<h6>Review your data:</h6><br /><br />";
	for(i=1; i<6; i++){
		if(values[i] != ''){
		datA = datA + '<b>' + keys[i] + '</b>' + values[i] + '&nbsp;&nbsp;&nbsp;';
		}
	}
	var Lat = '';
	var Long = '';
	if(values[6] != ''){
		Lat = '<b>' + keys[6] + '</b>' + values[6] + '&deg; ' + values[7] + '&#39; ' + values[8] + '&nbsp;&nbsp;&nbsp;';
		Long = '<b>' + keys[7] + '</b>' + values[9] + '&deg; ' + values[10] + '&#39; ' + values[11];
		}
	var div = document.getElementById("demo_data")
	div.style.display = "block";
	document.getElementById('addAssoc').style.display = 'none';
	document.getElementById('next').style.display = 'none';
	document.getElementById('submission').style.display = 'block';
	document.getElementById('step3').style.display = 'block';
	document.getElementById('step2').style.display = 'none';
	if(Lat != ""){
	div.innerHTML = datA + Lat + Long + '<br /><br /><hr><br />';
	}
	else {div.innerHTML = datA + '<br /><br /><hr><br />';}
	}

var counter = 1;
var limit = 50;
var gene = ['2DL1','2DL2','2DL3','2DL4','2DL5','2DP1','2DS1','2DS2','2DS3','2DS4','2DS5','3DL1','3DL2','3DL3','3DP1','3DS1','A3','A11','Bw4','Bw4(80I)','C1','C2'];

function pntAssoc(){

	function fieldVal(){
	var odds = parseFloat(document.getElementById("odds"+counter).value);
	var pvalue = parseFloat(document.getElementById("pvalue"+counter).value);
	var statest = document.getElementById("stat"+counter).value;
	if(odds === 0 || pvalue === 0 || statest === ""){
		return false;
		}
}
var fieldval = fieldVal();
var blank = 0;
var error = 0;
var i=0;
var dropdown = document.getElementById("my"+counter+"fs").getElementsByTagName("select");
for(i=0; i<17; i++){
	if(dropdown[i].value === ''){
		blank = blank + 1
		}
	}
for(i=1; i<17; i++){
	if(dropdown[0].value != '' && dropdown[i].value != ''){
		error = error + 1;
		}
	}
	
	if(blank === 17 || error > 0){
	alert('You must select at least one KIR gene or KIR haplogroup!');
		}
	else if(fieldval === false){
	alert("Odds Ratio, P Value and Statistical Test are mandatory fields.");
		}	
	else{
	var i=0;
	var genKIR='';
	var genHLAlig='';
	var freq='';
	var stat1='';
	var inps = ['Controls: ','Special Controls: ','Patients: ','Odds Ratio: ','P-Value: ']
	var tagSel = document.getElementById('my'+counter+'div').getElementsByTagName('select');
	var tagInp = document.getElementById('my'+counter+'div').getElementsByTagName('input');
	for(i=1; i<17; i++){
		if(tagSel[i].value!=""){
		genKIR=genKIR + gene[i-1] + "[" + tagSel[i].value + "]&nbsp;&nbsp;";
		}
		}
	for(i=17; i<gene.length; i++){
		if(tagSel[i].value!=""){
		genHLAlig=genHLAlig + gene[i-1] + "[" + tagSel[i].value + "]&nbsp;&nbsp;";
		}
		}
	for(i=0; i<3; i++){
		if(tagInp[i].value!=""){
		freq=freq + inps[i] + tagInp[i].value + "&nbsp;&nbsp;&nbsp;";
		}
	}
	for(i=3; i<inps.length; i++){
		if(tagInp[i].value!=""){
		stat1=stat1 + inps[i] + tagInp[i].value + "&nbsp;&nbsp;&nbsp;";
		}
	}
	if(tagSel[tagSel.length-2].value === "other"){
		var statest = document.getElementById("new_stat"+counter).value
		}
	else{
		var statest = tagSel[tagSel.length-2].value
		}
	if(tagSel[tagSel.length-1].value === "other"){
		var corr = document.getElementById("new_corr"+counter).value
		}
	else{
		var corr = tagSel[tagSel.length-1].value
		}
	if(tagSel[0].value != ""){
		genKIR = "Haplogroup = " + tagSel[0].value
		}
	var stat2 = 'Statistical Test: ' + statest + '&nbsp;&nbsp;&nbsp;Correction: ' + corr;
	
	document.getElementById("assoc"+counter).style.display = "block";
	document.getElementById("addAssoc").style.display = "block";
	document.getElementById("next").style.display = "block";
	document.getElementById('my'+counter+'div').style.display = 'none';
		if(genHLAlig != ''){
		document.getElementById("assoc"+counter).innerHTML="<hr /><b>ASSOCIATION "+counter+":</b></br></br><b><i>KIR genes or haplogroup:</i></b></br></br>"+
		genKIR+'<br /><br /><b><i>KIR Ligands:</i></b></br></br>'+genHLAlig+'<br /><br /><b><i>Frequency:</i></b></br></br>'+freq+'<br /><br /><b><i>Statistical Analysis:</i></b></br></br>'+stat1+stat2+'<br /><br />';
		}
		else
		document.getElementById("assoc"+counter).innerHTML="<hr /><b>ASSOCIATION "+counter+":</b></br></br><b><i>KIR genes or haplogroup:</i></b></br></br>"+
		genKIR+'<br /><br /><b><i>Frequency:</i></b></br></br>'+freq+'<br /><br /><b><i>Statistical Analysis:</i></b></br></br>'+stat1+stat2+'<br /><br />';
	}
}

function CancelInp() {
var newdiv = document.getElementById('my'+counter+'div');
var pntdiv = document.getElementById('assoc'+counter);
newdiv.parentNode.removeChild(newdiv);
pntdiv.parentNode.removeChild(pntdiv);
counter--;
document.getElementById('counter').value = counter
document.getElementById('addAssoc').style.display = 'block';
document.getElementById('next').style.display = 'block';

}


function addInput(div1, div2){
		if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " associations");
     }
     	else {
		 counter++;
         var newdiv = document.createElement('div');
		 var pntdiv = document.createElement('div');
		 var kirLabel = '<tr>'
		 var kirSel = '<tr>'
		 var hlaLabel = '<tr>'
		 var hlaSel = '<tr>'
		 newdiv.setAttribute('id','my'+counter+'div');
		 pntdiv.setAttribute('id','assoc'+counter);
		 pntdiv.setAttribute('style','margin:10px; display:none');
		 
		 for(i=0; i<16; i++){
			 kirLabel = kirLabel + '<td>' + gene[i] + '</td>'
		 }
		 for(i=0; i<16; i++){
			 kirSel = kirSel + "<td><select name='g"+gene[i]+"_"+counter+"' id='g"+gene[i]+"_"+counter+"'><option value=''>-</option><option value='1'>1</option><option value='0'>0</option></select></td>"
		 }
		 gene[19] = "Bw4<br />(80I)";
		 for(i=16; i<gene.length; i++){
			 hlaLabel = hlaLabel + '<td>' + gene[i] + '</td>'
		 }
		 for(i=16; i<gene.length; i++){
			 hlaSel = hlaSel + "<td><select name='g"+gene[i]+"_"+counter+"' id='g"+gene[i]+"_"+counter+"'><option value=''>-</option><option value='1'>1</option><option value='0'>0</option></select></td>"
		 }
		 var val = "'0.00'";
		 var empty = "''";
		 var stat = "'stat"+counter+"'";
		 var addstat = "'add_stat"+counter+"'"
		 var corr = "'corr"+counter+"'";
		 var addcorr = "'add_corr"+counter+"'"
		 var clickclear = '"clickclear(this,'+val+','+empty+');"';
		 var clickrecall = '"clickrecall(this,'+val+');"';
		 var checkStat = '"fctCheckValues('+stat+','+addstat+');"'
		 var checkCorr = '"fctCheckValues('+corr+','+addcorr+');"'
		 newdiv.innerHTML = "<fieldset id='my"+counter+"fs'><legend><b>Add your KIR and Disease Association</b></legend><p style='text-indent:0'><i>You can select Haplogroup (AA or Bx) <b>OR</b> KIR genes combinations. You can choose any KIR genes combination (from single KIR gene to a full genotype). You can also select KIR ligands in combination with KIR genes <b>OR</b> Haplogroups.</i></p><table><tr><td colspan='16'><i><b>KIR Haplogroup:</b></i></td></tr><tr><td colspan='16'><select name='haplogroup"+counter+"' id='haplogroup"+counter+"'><option value=''>select</option><option value='AA'>AA</option><option value='Bx'>Bx</option></select></td></tr><tr><td></td></tr><tr><td colspan='16'><i>KIR Genes:</i></tr>"+kirLabel+"</tr>"+kirSel+"</tr><tr><td></td></tr><tr><td colspan='16'><i><b>HLA Class I ligands:</b></i></td></tr>"+hlaLabel+"</tr>"+hlaSel+"</tr></table><br /><p style='text-indent:0'><i><b>Gene Frequency:</b></i></p><label for='gfc"+counter+"'>Controls:</label><input type='text' id='gfc"+counter+"' name='gfc"+counter+"' size='6' maxlength='10' value='0.00' onclick="+clickclear+" onKeypress='return fctDecimalVal();' onblur="+clickrecall+" />&nbsp;&nbsp;&nbsp;<label for='gfsc"+counter+"'>Special Controls:</label><input type='text' id='gfsc"+counter+"' name='gfsc"+counter+"' size='6' maxlength='10' value='0.00' onclick="+clickclear+" onKeypress='return fctDecimalVal();' onblur="+clickrecall+" />&nbsp;&nbsp;&nbsp;<label for='gfp"+counter+"'>Patients:</label><input type='text' id='gfp"+counter+"' name='gfp"+counter+"' size='6' maxlength='10' value='0.00' onclick="+clickclear+" onKeypress='return fctDecimalVal();' onblur="+clickrecall+" />&nbsp;&nbsp;&nbsp;</br><p style='text-indent:0'><i>Statistic Data:</i></p><label for='odds"+counter+"'>Odds Ratio:</label><input type='text' id='odds"+counter+"' name='odds"+counter+"' size='6' maxlength='10' value='0.00' onclick="+clickclear+" onKeypress='return fctDecimalVal();' onblur="+clickrecall+" />&nbsp;&nbsp;&nbsp;<label for='pvalue"+counter+"'>P-value:</label><input type='text' id='pvalue"+counter+"' name='pvalue"+counter+"' size='6' maxlength='10' value='0.0' onKeypress='return fctDecimalVal();' /><br /><br /><label for='stat"+counter+"'>Statistical Test:</label><select id='stat"+counter+"' name='stat"+counter+"' onchange="+checkStat+"><option value=''>select</option><option value='Chi-square'>Chi-square</option><option value='Fisher&#39;s Exact'>Fisher&#39;s Exact</option><option value='other'>other</option></select>&nbsp;&nbsp;&nbsp;<label for='corr"+counter+"'>Correction:</label><select id='corr"+counter+"' name='corr"+counter+"' onchange="+checkCorr+"><option value='None'>None</option><option value='Bonferroni'>Bonferroni</option><option value='Yates'>Yates</option><option value='Svejgaard and Ryder'>Svejgaard and Ryder</option><option value='other'>other</option></select><span id='add_stat"+counter+"' style='display:none'><br /><i>Please specify Statistical Test: </i><input name='new_stat"+counter+"' type='text' id='new_stat"+counter+"' size='30' maxlength='80'/></span><span id='add_corr"+counter+"' style='display:none'><br /><i>Please specify Statistical Correction: </i><input name='new_corr"+counter+"' type='text' id='new_corr"+counter+"' size='30' maxlength='80'/></br></span><br /><br /><p style='text-indent:0'><i><b>After filling all fields above, click on &quot;Save Association&quot;. </b></i><input type='button' value='Save Association' onClick='pntAssoc(); passAssocs();'>&nbsp;&nbsp;&nbsp;<input type='button' value='Reset' onclick='rstFields();' /></p><p style='text-indent:0'><i>Click on &quot;Cancel&quot; if you do not want to add the current association.</i>&nbsp;&nbsp;&nbsp;<input type='button' value='Cancel' onclick='CancelInp();' /></fieldset>";
		 document.getElementById(div1).appendChild(newdiv);
		 document.getElementById(div2).appendChild(pntdiv);
		 document.getElementById('counter').value = counter
		 document.getElementById('addAssoc').style.display = 'none';
		 document.getElementById('next').style.display = 'none';
		 gene[19] = 'Bw4(80I)';
     }
}


function cbCount(elem) {

var sValue = document.getElementById("n_assoc").value;
	for(i=1; i<=sValue; i++){
	var assocType = document.getElementById("assoc_type("+i+")").value;	
	if (assocType === "Single"){
		var theItem = document.getElementById(i);
		var theInputs = theItem.getElementsByTagName('input');
			var NewCount = 0
			for(var y=0; y<theInputs.length; y++){
	  		if(theInputs[y].type == 'checkbox'){
				if (theInputs[y].checked){NewCount = NewCount + 1}		
			}
			}
			if (NewCount == 2){
			alert('Pick Just One Please')
			for (x=0; x<theInputs.length; x++)
			theInputs[x].checked = false ;
			}

			
//alert(theItem);
//var eID = elem.id
//var assocType = document.getElementById("assoc_type("+eID+")").value;

//if (assocType === "Single"){

	//var NewCount = 0
	//if (document.form_dis.g2DL1_1.checked)
	//{NewCount = NewCount + 1}
	//if (document.form_dis.g2DL2_1.checked)
	//{NewCount = NewCount + 1}
	
}}}

function fctCheckValues(sel, inp)
{	
	var sValue = document.getElementById(sel).value;	
		
	if (sValue == "other")
	{
		document.getElementById(inp).style.display = "block";		
	}
	else
	{
		document.getElementById(inp).style.display = "none";		
	}
}

{
function fctNumVal()
  {
tecla = event.keyCode;
if (tecla >= 48 && tecla <= 57)
    {
    return true;
    }
else
    {
    return false;
    }
  }
}

function ValidaEmail()
{
  var obj = eval("document.forms[0].dis_email");
  var txt = obj.value;
  var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
  var check=/@[\w\-]+\./;
  var checkend=/\.[a-zA-Z]{2,3}$/;
  if(((txt.search(exclude) != -1)||(txt.search(check)) == -1)||(txt.search(checkend) == -1))
  {
    alert('Invalid E-mail!');
	obj.focus();
  }
}

function valMandatoryFields(div){
            var error=0;
            var mandatory;           
            for (i=0;i<div.length;i++){
                        mandatory = div[i].lang;
                        if (mandatory==1){
                                   if (div[i].value == ""){
                                               var name = div[i].name;
                                               changeFieldColor(div[i], 'red');

                                               error++;
                                   }
                        }
            }
            if(error>=1){
                        alert("There are " + error + " empty mandatory fields! ")
                        return false;
            } else
                        return true;
}

function changeFieldColor(element, color){
            element.style.backgroundColor=color;
}
